package com.smh.domain;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class Poker extends JLabel implements MouseListener {

    //牌的名字
    private String name;
    //正反
    private boolean up;
    //是否可以被点击
    private boolean canClick;
    //是否已被点击
    private boolean clicked;

    public Poker (String name,boolean up){
        this.name=name;
        this.up=up;
        if (up){
            //显示正面
            turnFront();
        }else {
            //显示反面
            turnRear();
        }

        //设置宽高
        this.setSize(71,96);
        //显示
        this.setVisible(true);
        //添加监听
        this.addMouseListener(this);
    }

    //显示正面
    public void turnFront(){
        this.setIcon(new ImageIcon("farmerAndLord\\image\\poker\\"+name+".png"));
        this.up=true;
    }

    //显示反面
    public void turnRear(){
        this.setIcon(new ImageIcon("farmerAndLord\\image\\poker\\rear.png"));
        this.up=false;
    }


    @Override
    public void mouseClicked(MouseEvent e) {
        System.out.println("点了");
        //点击
        if (canClick){
            //位移像素
            int step=0;
            if (clicked){
                step=20;
            }else {
                step=-20;
            }
            clicked=!clicked;

            //控制位移
            Point from = this.getLocation();
            Point to = new Point(from.x,from.y+step);
            this.setLocation(to);
            System.out.println("移动了");
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }

    public boolean isUp() {
        return up;
    }

    public void setUp(boolean up) {
        this.up = up;
    }

    public boolean isCanClick() {
        return canClick;
    }

    public void setCanClick(boolean canClick) {
        this.canClick = canClick;
    }

    public boolean isClicked() {
        return clicked;
    }

    public void setClicked(boolean clicked) {
        this.clicked = clicked;
    }

    @Override
    public String toString() {
        return "Poker{" +
                "name='" + name + '\'' +
                ", up=" + up +
                ", canClick=" + canClick +
                ", clicked=" + clicked +
                '}';
    }
}
